package com.utilities;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * This defines some of the utility methods that are might be common across classes. 
 * 
 * @author Parit Bansal
 *
 */
public class UtilMethods 
{
	/**
	 * Checks if the provided string is null or empty
	 * @param str
	 * @return
	 */
	public static boolean isNullOrEmpty (String str)
	{
		return (str==null || str.equalsIgnoreCase("")); 
	}
	
	public static boolean isAnyNullOrEmpty(List<String> strs)
	{
		for (String str : strs)
		{
			if (isNullOrEmpty(str)) return true;
		}
		return false;
	}
	
	/**
	 * copies the src file to the target location
	 * @param src
	 * @param target
	 * @throws IOException
	 */
	public static void copy (File src, File target) throws IOException
	{
		InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(target);
    
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) 
        {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
	}
	
	public static String extractFileName(String filePath, boolean extension)
	{
		filePath = filePath.replaceAll("\\\\", "/");
		if (extension)
		{
			return filePath.substring(filePath.lastIndexOf("/")+1,filePath.lastIndexOf("."));
		}
		else
		{
			return filePath.substring(filePath.lastIndexOf("/")+1);
		}
	}
}//UtilMethods
